package com.jinyou.web.filters;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RightFilter implements Filter {

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        // 1. ServletRequest 转成 HttpServletRequest
        HttpServletRequest req = (HttpServletRequest) request;
        // 2. 如果是GET，就生成�?��Wrapper对象，覆盖获取参数的方法进行转码
        if ("GET".equalsIgnoreCase(req.getMethod())) {
            req = new MyOwnEncode(req);
        }
        // 3. 如果是POST，request.setCharacterEncoding("UTF-8")
        if ("POST".equalsIgnoreCase(req.getMethod())) {
            req.setCharacterEncoding("UTF-8");
        }

//        Enumeration rnames = request.getParameterNames();
//        System.out.println("URL:" + req.getRequestURI());
//        for (Enumeration e = rnames; e.hasMoreElements(); ) {
//            String thisName = e.nextElement().toString();
//            String thisValue = request.getParameter(thisName);
//            System.out.println(thisName + "-------" + thisValue);
//        }
//        System.out.println();
//
        HttpServletResponse res = (HttpServletResponse) response;
        //解决跨域问题
        res.addHeader("Access-Control-Allow-Origin", "*");

        chain.doFilter(req, res);

    }

    public void destroy() {

    }


    //更改接受到的数据的编码方式
    class MyOwnEncode extends HttpServletRequestWrapper {

        public MyOwnEncode(HttpServletRequest request) {
            super(request);
        }

        public String changeEncoding(String input, String srcEncoding, String targetCoding) {
            try {
                byte[] data = input.getBytes();
                return new String(data, targetCoding);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return input;
        }

        @Override
        public String getParameter(String name) {
            String value = super.getParameter(name);
            if (value != null) {
                value = changeEncoding(value, "ISO-8859-1", "UTF-8");
            }
            return value;
        }

        @Override
        public String[] getParameterValues(String name) {
            String[] values = super.getParameterValues(name);
            if (values != null && values.length > 0) {
                for (int i = 0; i < values.length; i++) {
                    String value = values[i];
                    values[i] = changeEncoding(value, "ISO-8859-1", "UTF-8");
                }
            }
            return values;
        }
    }
}
